package demo2;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class UseLink {
    public static void main(String[]args){
        List<Integer> list1=new LinkedList<>();
        list1.add(0);
        list1.add(1);
        list1.add(2);
        list1.add(3);
        list1.add(4);
        list1.add(5);
        List<Integer> list2=new LinkedList<>();
        list2.add(8);
        list2.add(6);
        list2.add(0);
        list2.addAll(list1);
        System.out.println(list2);
        //Linklist的遍历
        Iterator<Integer> it=list2.iterator();
        while(it.hasNext()){    //下一个还有就打印
            System.out.print(it.next()+" ");//迭代器打印
        }
        System.out.println();
        //Iterator的子类ListIterator
        ListIterator<Integer> lit=list2.listIterator();
        while(lit.hasNext()){
            System.out.print(lit.next()+" ");
        }
        System.out.println();
        //ListIterator的重载方法可在括号里指定位置打印
        ListIterator<Integer> lit2=list2.listIterator(2) ;
        while (lit2.hasPrevious()){
            System.out.print(lit2.previous()+" ");//从后往前打印
        }
        System.out.println();
    }

}
